/**
 * Created by JetBrains WebStorm.
 * User: Tonis
 * Date: 20.02.12
 * Time: 17:22
 * To change this template use File | Settings | File Templates.
 */
PlayerType = {
    COMPUTER : "COMPUTER",
    HUMAN : "HUMAN"
};

AIStates = {
    ESCAPE: "ESCAPE",
    TRAP: "TRAP",
    HUNT: "HUNT",
    POWERUP: "POWERUP",
    FOLLOW: "FOLLOW"
};

GameModeSelection = {
    PLAYERvsPLAYER : "PLAYERvsPLAYER",
    PLAYERvsAI : "PLAYERvsAI",
    MultiPLAYER : "MultiPLAYER"
};

GameModeSelectionKEYS = {
    F1 : "112",
    F2 : "113",
    F3 : "114"
};

KeyBoardType = {
    WASD : "WASD",
    ARROWS : "ARROWS"
};
ArrowKeysEnums = {
    LEFT: 37,
    UP: 38,
    RIGHT: 39,
    DOWN: 40,
    PLANT: 76 // l
};

WASDEnums = {
    LEFT : 65,
    UP : 87,
    RIGHT : 68,
    DOWN : 83,
    PLANT: 71 // g
};

ControlKeysEnums = {
    CTRL : 17
};
function loadImage(name) {
    images[name] = new Image();
    images[name].onload = function () {
        resourceLoaded();
    };
    images[name].src = "img/" + name + ".png";
}

function resourceLoaded() {
    loadingReady();
    numResourcesLoaded += 1;
    if(numResourcesLoaded == totalResources) {
        contentLoaded = true;
    }
}

function updatePlayerSidebar(player){
    jQuery('#guest' + player.getID() + ' #lives' ).text('Lives ' + player.lives);
    jQuery('#guest' + player.getID() + ' #score' ).text('Score ' + player.kills);
}

function createPlayerSideBar(player) {

    var id = player.getID();
    try {

        jQuery('<div/>', {
            id:'guest' + id,
            class:'guest'
        }).appendTo('#canvasRightWrapper');
        jQuery("<h1>Guest " + id + "</h1>").appendTo('#guest' + id);
        jQuery('<img/>', {
            src: "img/fb_profile_default.png",
            class: 'profile_pic'
        }).appendTo('#guest' + id);
        jQuery('<div/>', {
            id: "lives"
        }).appendTo('#guest' + id);
        jQuery('#guest' + id + ' #lives' ).text('Lives ' + player.lives);
        jQuery('<div/>', {
            id: "score"
        }).appendTo('#guest' + id);
        jQuery('#guest' + id + ' #score' ).text('Score ' + player.kills);
    } catch (ex) {
        alert('error in jQuery call:' + ex);
    }
}

function getMouse(e) {
    var element = this.canvas, offsetX = 0, offsetY = 0, mx, my;

    // Compute the total offset
    if (element.offsetParent !== undefined) {
        do {
            offsetX += element.offsetLeft;
            offsetY += element.offsetTop;
        } while ((element = element.offsetParent));
    }

    mx = e.pageX - offsetX;
    my = e.pageY - offsetY;

    // We return a simple javascript object (a hash) with x and y defined
    return {x:mx, y:my};
}
function drawGridObject(spriteSheet,sx, sy, x, y) {
	try {
		ctx.drawImage(spriteSheet, sx * maxGrid, sy * maxGrid, maxGrid, maxGrid, x, y, maxGrid, maxGrid);
	} catch(e) {
		alert(e);
	}
}

/**
 * checks if place is free of bombs and walls
 * 
 * @param x
 * @param y
 * @returns {Boolean}
 */
function isPlaceFree(x, y) {
    try{
        var t = level.map[x][y];
        if (t.length > 0) {
            switch (t.charAt(0)) {
                case 'w':
                    return false;
                case 'e':
                    return false;
                case 'h':
                    return false;
            }
        }
    }catch(err){return false;}

    if (getItemIndexByPos(bombs, x, y) > -1) {
    	return false;
    }
    return true;
}

var uniqueID = (function() {
    var id = 0; // This is the private persistent value
    // The outer function returns a nested function that has access
    // to the persistent value.  It is this nested function we're storing
    // in the variable uniqueID above.
    return function() { return id++; };  // Return and increment
})(); // Invoke the outer function after defining it.

/**
 * @param items array of objects which as getPosX and getPosY getters
 * @param x
 * @param y
 * @returns object or -1 if not found
 */
function getItemByPos(items, x, y) {
	index = getItemIndexByPos(items, x, y);
	if (index != -1) {
		return items[index];
	}
	return index;
}

/**
 * @param items array of objects which as getPosX and getPosY getters
 * @param x
 * @param y
 * @returns {Number} if not found -1
 */
function getItemIndexByPos(items, x, y) {
	for (var i = 0; i < items.length; i ++) {
		if (items[i] != null || items[i] != undefined) {
			if (items[i].getPosX() == x && items[i].getPosY() == y) {
				return i;
			}
		}
	}
	return -1;
}